package com.woniuxy.ams.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.Dept;
import com.woniuxy.ams.entity.PageRequest;
import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.service.DeptService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author fyx
 */
@RestController
    @RequestMapping("/dept")
public class DeptController {
    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @PostMapping("/findAll")
    public ResultEntity<?> findAllUserByPage(@RequestBody PageRequest<Dept> pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getLimit());
        List<Dept> depts = deptService.findAllDeptAndSearch(pageRequest.getData());
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        return new ResultEntity<>(200,"查询成功",pageInfo);
    }

    @PostMapping("/addOne")
    public ResultEntity<?> addDept(@RequestBody Dept dept) {
      boolean isSuccess = deptService.addDept(dept);
        if (isSuccess) {
            return new ResultEntity<>(200, "添加成功");
        }
        return new ResultEntity<>(200, "添加失败");
    }

    @PostMapping("/findAllDept")
    public ResultEntity<List<Dept>> findAllDept(){
        List<Dept> depts = deptService.findAllDeptAndSearch(null);
        if (depts.size() != 0) {
            return new ResultEntity<>(200, "查询成功", depts);
        }else {
            return new ResultEntity<>(400, "查询失败",null);
        }
    }

    @PostMapping("/findOne")
    public ResultEntity<?> findOneById(@RequestBody Dept dept) {
        Dept deptResult = deptService.findOneByIdOrName(dept);
        if (dept != null) {
            return new ResultEntity<>(200, "查询成功", deptResult);
        }
        return new ResultEntity<>(403,"该部门不存在");
    }
}
